<?php    
    header("Content-Type: application/json");
    
    require(__DIR__.
        DIRECTORY_SEPARATOR.
        "inc".
        DIRECTORY_SEPARATOR.
        "incFunctions.php"
    );
    
    $class      = null;
    $function   = null;    
    $parts      = explode("/",arrGet($_SERVER,"REQUEST_URI",""));
    $count      = count($parts);
    
    # Haetaan class ja function
    if ($count >= 2) {
        $class      = $parts[$count-2];
        $function   = $parts[$count-1];
    } else if (isset($_SERVER["argv"][1],$_SERVER["argv"][2])) {
        $class      = $_SERVER["argv"][1];
        $function   = $_SERVER["argv"][2];    
    }
    
    # Löytyykö sessionid header    
    if (isset($_SERVER["HTTP_X_SESSIONID"])) {
        LoginControl::set("sessionid",$_SERVER["HTTP_X_SESSIONID"]);
    } else if (arrGet($_SERVER,"argc",0) > 2 && getenv("HTTP_X_SESSIONID")) {
        LoginControl::set("sessionid",getenv("HTTP_X_SESSIONID"));
    }
    
    # Haetaan kutsun data    
    $data = array();
    $input = false;
    if (arrGet($_SERVER,"REQUEST_METHOD") == "POST") {
        $input = file_get_contents("php://input");
    } else if (isset($_SERVER["argv"][3]) && file_exists($_SERVER["argv"][3])) {
        $input = file_get_contents($_SERVER["argv"][3]);
    }
    
    # JSON dekoodaus
    if ($input) {
        $data = json_decode($input,true);
    }
    
    # Prosessoi kutsu
    echo json_encode(RequestHandler::process($class,$function,$data));